package com.adtech.adms.commons.file.web.convertor;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.util.StrutsTypeConverter;

import com.adtech.adms.commons.file.pojo.FileInfo;

/**
 * 
 * 
 * @author 门宏建 
 * @since 2011-8-11 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class FileListConvertor extends StrutsTypeConverter {

	@Override
	public Object convertFromString(Map context, String[] values, Class c) {

		List<FileInfo> list = new ArrayList<FileInfo>();
		if (values != null && values.length > 0) {

			for (String fileJson : values) {

				try {
					fileJson = URLDecoder.decode(fileJson.replace("%", "$#$").replace("&", "$##$").replace("+", "$###$"), "utf-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				JSONObject jsonObj = JSONObject.fromObject(fileJson);
				String realPath = jsonObj.getString("realPath");
				String realName = jsonObj.getString("realName").replace("$#$", "%").replace("$##$", "&").replace("$###$", "+");
				String fileSize = jsonObj.getString("fileSize");
				FileInfo model = new FileInfo();
				model.setRealName(realName);
				model.setRealPath(realPath);
				model.setFileSize(fileSize);

				list.add(model);
			}

		}

		return list;

	}

	@Override
	public String convertToString(Map arg0, Object obj) {
		return obj.toString();
	}

}
